﻿var viweModel = function () {
    var me = this;
    me.form = {
        user_code: ko.observable(),
        password: ko.observable()
    };
    me.message = ko.observable();

    me.login = function (form) {
        console.log($("#loginForm"));
        if (!$("#loginForm").form("validate")) {
            me.message("Please input the required field!");
            return;
        }
        $.ajax({
            type: "POST",
            url: "/login/DoAction",
            data: ko.toJSON(me.form),
            dataType: "json",
            contentType: "application/json",
            success: function (ret) {
                if (ret.status === "success") {
                    me.message("login succeed, transfering,wait ...");
                    window.location.href = "/";
                } else {
                    me.message(ret.message);
                }
            },
            error: function (e) {
                me.message(e.responseText);
            },
            beforeSend: function () {
                $(form).find("input").attr("disabled", true);
                me.message("logining, wait ...");
            },
            complete: function () {
                $(form).find("input").attr("disabled", false);
            }
        });
    };

    me.reset = function () {
        me.form.user_code("");
        me.form.password("");
    };

    me.init = function () {
        if (top != window) {
            alert("top is not a window!");
            top.window.location = window.location;
        }
    };
    this.init();
}

$(function () { ko.applyBindings(new viweModel()); });